package com.aube.guide.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.aube.R;
import com.aube.guide.DoubleRect;
import com.aube.guide.OnGuidePerformedListener;
import com.aube.guide.ViewTarget;

/**
 * Created by huyaonan on 16/6/22.
 */
public class DragGuideView extends BaseGuideView {

    /**
     * rectShape focus on target
     * and clear rect to focus
     */
    private DoubleRect rectShape;

    /**
     * All views will be drawn to
     * this bitmap and canvas then
     * bitmap will be drawn to canvas
     */
    protected Bitmap bitmap;
    protected Canvas canvas;

    /**
     * Eraser
     */
    private Paint eraser;

    /**
     * Info dialog view
     */
    private View infoView;

    /**
     * Info ImageView
     */
    private ImageView imageView;

    public DragGuideView(Context context) {
        this(context, null);
    }

    public DragGuideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragGuideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        eraser = new Paint();
        eraser.setColor(0xFFFFFFFF);
        eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        eraser.setFlags(Paint.ANTI_ALIAS_FLAG);


        View layoutInfo = LayoutInflater.from(getContext()).inflate(R.layout.guide_layout, null);

        infoView = layoutInfo.findViewById(R.id.guide_layout);
        imageView = (ImageView) layoutInfo.findViewById(R.id.guide_imageview);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                rectShape.reCalculateAll();
                if (rectShape != null && rectShape.getPoint().y != 0 && !isLayoutCompleted) {
                    if (isInfoEnabled)
                        setInfoLayout();
                    removeOnGlobalLayoutListener(DragGuideView.this, this);
                }
            }
        });
    }

    @Override
    protected void drawGuide(Canvas canvas) {
        if (!isReady) return;

        if (bitmap == null || canvas == null) {
            if (bitmap != null) bitmap.recycle();

            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            this.canvas = new Canvas(bitmap);
        }

        /**
         * Draw mask
         */
        this.canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        this.canvas.drawColor(maskColor);

        /**
         * Clear focus area
         */
        rectShape.draw(this.canvas, eraser, padding);

        canvas.drawBitmap(bitmap, 0, 0, null);
    }

    @Override
    protected void showInfo() {
        if (infoView.getParent() != null)
            ((ViewGroup) infoView.getParent()).removeView(infoView);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageRes);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Rect rect = rectShape.getRect();

        LayoutParams infoDialogParams = new LayoutParams(width, height);
        int marginTop = (rect.bottom + rect.top)/2 - height/2;
        int marginLeft = rect.left - width;
        infoDialogParams.setMargins(marginLeft, marginTop, 0, 0);

        infoView.setLayoutParams(infoDialogParams);
        infoView.postInvalidate();

        addView(infoView);

        imageView.setImageBitmap(bitmap);

        infoView.setVisibility(VISIBLE);
    }

    private void setRect(DoubleRect rectShape) {
        this.rectShape = rectShape;
    }

    /**
     * Builder Class
     */
    public static class Builder {

        private DragGuideView dragGuideView;

        private Activity activity;

        public Builder(Activity activity) {
            this.activity = activity;
            dragGuideView = new DragGuideView(activity);
        }

        public Builder setMaskColor(int maskColor) {
            dragGuideView.setMaskColor(maskColor);
            return this;
        }

        public Builder setDelayMillis(int delayMillis) {
            dragGuideView.setDelay(delayMillis);
            return this;
        }

        public Builder enableFadeAnimation(boolean isFadeAnimationEnabled) {
            dragGuideView.enableFadeAnimation(isFadeAnimationEnabled);
            return this;
        }

        public Builder setTarget(View view) {
            dragGuideView.setTarget(new ViewTarget(view));
            return this;
        }

        public Builder setMainView(View view) {
            dragGuideView.setMainView(new ViewTarget(view));
            return this;
        }

        public Builder setTargetPadding(int padding) {
            dragGuideView.setPadding(padding);
            return this;
        }

        public Builder dismissOnTouch(boolean dismissOnTouch) {
            dragGuideView.setDismissOnTouch(dismissOnTouch);
            return this;
        }

        public Builder setUsageId(String dragGuideViewId) {
            dragGuideView.setUsageId(dragGuideViewId);
            return this;
        }

        public Builder setImageRes(int imageRes) {
            if(imageRes != 0) {
                dragGuideView.enableInfoDialog(true);
                dragGuideView.setImageRes(imageRes);
            }
            return this;
        }

        public Builder setListener(OnGuidePerformedListener mPerformedListener) {
            dragGuideView.setListener(mPerformedListener);
            return this;
        }

        public Builder performClick(boolean isPerformClick){
            dragGuideView.setPerformClick(isPerformClick);
            return this;
        }

        public Builder setCanAudoDismiss(int time) {
            dragGuideView.setCanAudoDismiss(time);
            return this;
        }

        public DragGuideView build() {
            int color = activity.getResources().getColor(R.color.theme);
            DoubleRect rect = new DoubleRect(
                    dragGuideView.targetView, dragGuideView.mainView,
                    dragGuideView.padding, color);
            dragGuideView.setRect(rect);
            return dragGuideView;
        }

        public DragGuideView show() {
            build().show(activity);
            return dragGuideView;
        }

    }

}